package com.wsoft.excel.converter;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 用于 Excel 导出 自定义注解 时间类型等格式转换
 * @Author z001
 * @Date 2024/11/7 09:29
 */
public class GenericConverter implements Converter<Object> {
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Class<Object> supportJavaTypeKey() {
        return Object.class;
    }

    @Override
    public WriteCellData<Object> convertToExcelData(WriteConverterContext<Object> context) throws Exception {
        Object value = context.getValue();
        if (ObjUtil.isEmpty(value)) {
            return new WriteCellData("");
        }
        Class<?> fieldType = context.getContentProperty().getField().getType();

        if (fieldType == Integer.class || fieldType == int.class) {
            return new WriteCellData<>(value.toString());
        } else if (fieldType == Double.class || fieldType == double.class) {
            return new WriteCellData<>(value.toString());
        } else if (fieldType == Date.class) {
            return new WriteCellData<>(  DATE_FORMAT.format(context.getValue()));
        } else if (fieldType == LocalDateTime.class) {
            return new WriteCellData<>(LocalDateTimeUtil.of(DateUtil.parse(value.toString())).format(DATE_TIME_FORMATTER));
        } else if (fieldType == String.class) {
            return new WriteCellData<>((String) value);
        }

        return new WriteCellData<>(value.toString());


    }

    @Override
    public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        Object data = cellData.getData();
        if (ObjUtil.isEmpty(data)) {
            return null;
        }
        String value = data.toString();

        Class<?> fieldType = contentProperty.getField().getType();

        if (fieldType == Integer.class || fieldType == int.class) {
            return Integer.parseInt(value);
        } else if (fieldType == Double.class || fieldType == double.class) {
            return Double.parseDouble(value);
        } else if (fieldType == Date.class) {
            return DateUtil.parse(value);
        } else if (fieldType == LocalDateTime.class) {
            return LocalDateTimeUtil.of(DateUtil.parse(value));
        } else if (fieldType == String.class) {
            return value;
        }

        return null;
    }

}
